package com.zian.controller.admin;


import com.zian.annotation.OperLog;
import com.zian.common.R;
import com.zian.domain.dto.CarouselDTO;
import com.zian.domain.dto.PageDTO;
import com.zian.domain.po.SysCarousel;
import com.zian.domain.query.PageQuery;
import com.zian.enums.BusinessType;
import com.zian.service.ISysCarouselService;
import com.zian.validation.ValidGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2024-09-08
 */
@Slf4j
@Api("轮播图接口")
@RestController("SysCarouselControllerAdmin")
@RequestMapping("/admin/carousels")
@RequiredArgsConstructor
public class SysCarouselController {

    private final ISysCarouselService carouselService;

    @PostMapping
    @ApiOperation("新增轮播图")
    @OperLog(title = "轮播图", businessType = BusinessType.INSERT)
    public R addCarousel(@RequestBody @Validated(ValidGroup.Crud.Create.class) CarouselDTO dto) {
        log.info("新增轮播图：{}", dto);
        carouselService.addCarousel(dto);
        return R.ok();
    }


    @GetMapping("/page")
    @ApiOperation("分页查询")
    public R<PageDTO<SysCarousel>> queryPage(PageQuery query) {
        log.info("分页查询参数：{}", query);
        return R.ok(carouselService.queryPage(query));
    }

    @PutMapping
    @ApiOperation("修改轮播图")
    @OperLog(title = "轮播图", businessType = BusinessType.UPDATE)
    public R updateCarousel(@RequestBody @Validated(ValidGroup.Crud.Update.class) CarouselDTO dto) {
        log.info("修改轮播图：{}", dto);
        carouselService.updateCarousel(dto);
        return R.ok();
    }

    @DeleteMapping
    @ApiOperation("删除轮播图")
    @OperLog(title = "轮播图", businessType = BusinessType.DELETE)
    public R deleteCarouselByIds(@RequestParam("ids") List<Long> ids) {
        log.info("删除轮播图数据：{}", ids);
        carouselService.deleteCarouselByIds(ids);
        return R.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation("查询轮播图")
    public R<CarouselDTO> getCarouselById(@PathVariable Long id) {
        log.info("查询轮播图数据：{}", id);
        return R.ok(carouselService.getCarouselById(id));
    }


}
